package com.yan.deezer.ui.fragment;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragment;
import com.yan.deezer.R;
import com.yan.deezer.data.manager.DataManager;
import com.yan.deezer.ui.adapter.ArtistAlbumListAdapter;
import com.yan.deezer.utils.Debug;

public class ArtistAlbumListFragment extends SherlockFragment {
	protected int mNum;
	protected Handler mHandler;
	protected DataManager mDataManager;
	private ListView artistAlbumListView;
	private ArtistAlbumListAdapter artistAlbumListAdapter;
	private TextView emptyView;
	private View loadingView;

	/**
	 * Create a new instance of package ArtistAlbumListFragment, providing "num"
	 * as an argument.
	 */
	public static ArtistAlbumListFragment newInstance(int num, String artistID) {
		ArtistAlbumListFragment f = new ArtistAlbumListFragment();

		// Supply num input as an argument.
		Bundle args = new Bundle();
		args.putInt("num", num);
		args.putString("ARTIST_ID", artistID);
		f.setArguments(args);

		return f;
	}

	/**
	 * When creating, retrieve this instance's number from its arguments.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mNum = getArguments() != null ? getArguments().getInt("num") : 1;
		setHasOptionsMenu(true);

		mHandler = new Handler();
		mDataManager = DataManager.getInstance();
		artistAlbumListAdapter = null;
		new InitAsyncTask().execute();

	}

	/**
	 * The Fragment's UI is just a simple text view showing its instance number.
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.album_list_screen, container, false);

		artistAlbumListView = (ListView) v.findViewById(R.id.album_listview);
		artistAlbumListView.setDividerHeight(0);
		loadingView = v.findViewById(R.id.loading);
		emptyView = (TextView) v.findViewById(R.id.empty);
		loadingView.setVisibility(View.GONE);
		artistAlbumListView.setEmptyView(loadingView);

		return v;
	}

	@Override
	public void onResume() {
		super.onResume();
		new InitAsyncTask().execute();

	}

	public void initArtistAlbums(String artistID) {

		try {
			if (artistAlbumListAdapter == null) {
				artistAlbumListAdapter = new ArtistAlbumListAdapter(
						getActivity().getApplicationContext(),
						mDataManager.getArtistAlbums(artistID),
						R.layout.album_list_element);
			} else {
				showArtistAlbumList();

			}
		} catch (Exception e) {

		}
	}

	public void showArtistAlbumList() {
		mHandler.post(new Runnable() {
			@Override
			public void run() {
				artistAlbumListView.setAdapter(artistAlbumListAdapter);
				artistAlbumListView
						.setOnItemClickListener(artistAlbumListAdapter);
				loadingView.setVisibility(View.GONE);
				artistAlbumListView.setEmptyView(emptyView);
			}
		});
	}

	/**
	 * 
	 * ASYNC TASK
	 * 
	 */

	private class InitAsyncTask extends AsyncTask<Void, Void, Void> {
		@Override
		public Void doInBackground(Void... notUsed) {

			initArtistAlbums((String) getArguments().get("ARTIST_ID"));
			return null;
		}

		@Override
		public void onPostExecute(Void notUsed) {
			showArtistAlbumList();

			Debug.getCurrentTimeMeasure(true);

		}

	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {

		super.onActivityCreated(savedInstanceState);
		setHasOptionsMenu(true);

	}

}